package com.ebelter.btlibrary.btble.impl.ict;

import com.ebelter.btlibrary.btble.ble.bluetooth.device.impl.BleCommandEditor;
import com.ebelter.btlibrary.btble.impl.ict.model.IctCommand;
import com.ebelter.btlibrary.util.ULog;

import java.util.Calendar;

/**
 * 血压计指令编辑器实现层
 * Created by Sergio Pan on 2017/12/8.
 */

public class IctCommondEditor extends BleCommandEditor {

    private static final String TAG = "IctCommondEditor";

    private static IctCommondEditor instance = new IctCommondEditor();

    private IctCommondEditor() {
    }

    public static IctCommondEditor getInstance() {
        return instance;
    }


    @Override
    protected void onReceiveCommand(Object command) {
        IctCommand ictCommand = (IctCommand) command;
        ULog.i(TAG, "-------onReceiveCommand-------ictCommand = " + ictCommand);
        switch (ictCommand){
            case SYNC_CLOCK:
              //  synchronizeBleClock();
                break;
                case RECEIVED:

                break;
        }
    }


    /**
     * 下发同步系统时间命令
     */
    public void synchronizeBleClock() {
        ULog.i(TAG, "send synchronize Ble Clock cmd.");
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);

        byte last = xor(new byte[]{0x02, 0x40, (byte) 0xdd, 0x09, (byte) 0xff, (byte) 0xfd, (byte) 0x01,
                (byte) (year % 100), (byte) month, (byte) day, (byte) hour, (byte) minute, (byte) second});
        ULog.i(TAG, "-------synchronizeBleClock-------date = year:" + (year % 100) + ", month:" + month + ", day:" + day + ", hour:" + hour + ", minute:" + minute + ", second:" + second);
        byte[] bytes = {
                0x02,
                0x40,
                (byte) 0xdd,
                0x09,//长度
                (byte) 0xff,
                (byte) 0xfd,
                (byte) 0x01,
                (byte) (year % 100),
                (byte) month,
                (byte) day,
                (byte) hour,
                (byte) minute,
                (byte) second,
                last
        };

        sendBleMessage(bytes);
    }


    /**
     * 开始测量
     */
    public void startMeasure() {
        ULog.i(TAG, "send start Measure cmd.");
//        0240DD04FFFD0201 98
        byte[] bytes = {
                0x02,
                0x40,
                (byte) 0xdd,
                0x04,
                (byte) 0xff,
                (byte) 0xfd,
                0x02,
                0x01,
                (byte) 0x98
        };
        sendBleMessage(bytes);
    }


    /**
     * 停止测量
     */
    public void stopMeasure() {
        ULog.i(TAG, "send stop Measure cmd.");
//        0240DD04FFFD0202  9B
        byte[] bytes = {
                0x02,
                0x40,
                (byte) 0xdd,
                0x04,
                (byte) 0xff,
                (byte) 0xfd,
                0x02,
                0x02,
                (byte) 0x9b
        };
        sendBleMessage(bytes);
    }


    /**
     * 停止设备发送
     */
    public void stopDeviceSend() {
        ULog.i(TAG, "send stop Device Send cmd.");
//        0240DD04FFFD0205  9C
        byte[] bytes = {
                0x02,
                0x40,
                (byte) 0xdd,
                0x04,
                (byte) 0xff,
                (byte) 0xfd,
                0x02,
                0x05,
                (byte) 0x9c
        };
        sendBleMessage(bytes);
    }

    /**
     * 关闭设备
     */
    public void powerOffDevice() {
        ULog.i(TAG, "send power Off Device cmd.");
//        0240DD04FFFD0204  9D
        byte[] bytes = {
                0x02,
                0x40,
                (byte) 0xdd,
                0x04,
                (byte) 0xff,
                (byte) 0xfd,
                0x02,
                0x04,
                (byte) 0x9d
        };
        sendBleMessage(bytes);
    }

}
